﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class CommentDAO : BaseService
    {
        /// <summary>
        /// 根据发布信息用户和状态查询任务信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <param name="userid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IList<Comment> GetListPaging(int page, int pagesize, int userid, string state, bool isDel)
        {
            string hql = "from Comment where 1=1 and IsDel=false and IsPub = false";
            if (state == "-99")
            {
                hql += " and IsUser = " + userid;
            }
            else
            {
                hql += " and IsUser = " + userid + " and State in ('" + state + "')";
            }
            hql += " and IsDel="+isDel+" order By AddDate Desc";
            return FindList<Comment>(hql, null, (page - 1) * pagesize, pagesize);
        }
        /// <summary>
        /// 根据发布信息用户和状态查询任务信息条目数
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int GetListPaging(int userid, string state, bool isDel)
        {
            string hql = "select count(*) from Comment where 1=1 and IsDel=false and IsPub = false";
            if (state == "-99")
            {
                hql += " and IsUser = " + userid;
            }
            else
            {
                hql += " and IsUser = " + userid + " and State in ('" + state + "')";
            }
            hql += " and IsDel=" + isDel;
            return Convert.ToInt32(GetValue(hql));
        }
        /// <summary>
        /// 根据处理意见用户和状态查询任务信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <param name="worker"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IList<Comment> GetListPagingByWorker(int page, int pagesize, int worker, string state)
        {
            string hql = "from Comment where 1=1";
            if (state == "-99")
            {
                hql += " and Worker =" + worker;
            }
            else
            {
                hql += " and Worker =" + worker + " and State in (" + state + ")";
            }
            hql += " and IsDel=false and IsPub = false order By AddDate Desc";
            return FindList<Comment>(hql, null, (page - 1) * pagesize, pagesize);
        }
        /// <summary>
        /// 根据处理意见用户和状态查询任务信息条目数
        /// </summary>
        /// <param name="worker"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int GetListPagingByWorker(int worker, string state)
        {
            string hql = "select count(*) from Comment where 1=1";
            if (state == "-99")
            {
                hql += " and Worker ="+worker;
            }
            else
            {
                hql += " and Worker =" + worker + " and State in (" + state+")";
            }
            hql += " and IsDel=false and IsPub = false " ;
            return Convert.ToInt32(GetValue(hql));
        }


        /// <summary>
        /// 查询待完成意见
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<Comment> GetListPagings(int state,int userid, bool isPub, bool isDel, int page, int pagesize)
        {
            string hql = "from Comment where 1=1 and State="+state+" and IsUser=" + userid + " and IsPub=" + isPub + " and IsDel = " + isDel + " order by AddDate desc";
            try
            {
                return FindList<Comment>(hql, null, (page - 1) * pagesize, pagesize);
            }
            catch (Exception)
            {
                return null;

            }
        }


        /// <summary>
        /// 查询待完成意见数量
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public int GetListPagings(int state,int userid, bool isPub, bool isDel)
        {
            string hql = "select count(*) from Comment where 1=1 and State = "+state+"  and IsUser=" + userid + " and IsPub=" + isPub + " and IsDel = " + isDel;
            try
            {
                return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {

                return 0;
            }
        }


        /// <summary>
        /// 查询已删除意见
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<Comment> GetListPagings( int userid,  bool isDel, int page, int pagesize)
        {
            string hql = "from Comment where 1=1 and IsUser=" + userid +  " and IsDel = " + isDel + " order by AddDate desc";
            try
            {
                return FindList<Comment>(hql, null, (page - 1) * pagesize, pagesize);
            }
            catch (Exception)
            {
                return null;

            }
        }


        /// <summary>
        /// 查询已删除意见数量
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public int GetListPagings(int userid,  bool isDel)
        {
            string hql = "select count(*) from Comment where 1=1  and IsUser=" + userid + " and IsDel = " + isDel;
            try
            {
                return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {

                return 0;
            }
        }
        /// <summary>
        /// 批量修改暂存
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchCommentUpdate(string ids)
        {
            string hql = "update Comment set IsPub = false where Id in ("+ids+")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量修改删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchCommentDelete(string ids)
        {
            string hql = "update Comment set IsDel = true where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量取消删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public Boolean BatchCCommentUpdate(string ids)
        {
            string hql = "update Comment set IsDel = false where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public Boolean BatchCCommentDelete(string ids)
        {
            string hql = "delete Comment where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

    }
}
